<?php


namespace Xiaoguai\DesignPattern\DP000;

use Xiaoguai\DesignPattern\Main\Base\CacheDriver\FileCache;
use Xiaoguai\DesignPattern\Main\Base\CacheDriver\RedisCache;
use Xiaoguai\DesignPattern\Main\Base\CacheDriver\SessionCache;
use Xiaoguai\DesignPattern\Main\Base\CacheInterface;

class CacheFactory
{
    /**
     * 简单工厂模式
     * 根据标识符来匹配创造对应的类和对象, 类和对象需要实现统一的接口
     * @param string $source
     * @return CacheInterface
     */
    public static function createCache(string $source = null): CacheInterface
    {
        $cache = null;
        switch ($source) {
            case 'redis':
                $cache = new RedisCache();
                break;

            case 'file':
                $cache = new FileCache();
                break;
            default:
                $cache = new SessionCache();
                break;
        }
        return $cache;
    }
}